#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;


void solve() {
    int n;
    cin >> n;
    vector<pii> a(n);
    vector<int> b;
    for(int i = 0; i < n; i++) {
        int l, r;
        cin >> l >> r;
        a[i] = {l, r};
        b.push_back(l), b.push_back(r);
    }
    sort(b.begin(), b.end());
    vector<int> c(n);
    for(int i = 0; i < n; i++) {
        auto [l, r] = a[i];
        int p1 = lower_bound(b.begin(), b.end(), l) - b.begin();
        int p2 = lower_bound(b.begin(), b.end(), r) - b.begin();
        int t = p1 + p2;
        if(l > r) t--;
        c[i] = t;
    }
    vector<int> idx(n);
    for(int i = 0; i < n; i++) idx[i] = i;
    sort(idx.begin(), idx.end(), [&](int a, int b) {
        return c[a] < c[b];
    });
    for(auto i : idx) {
        cout << a[i].first << ' ' << a[i].second << ' ';
    }
    cout << '\n';
}

int main() {
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}